package uy.edu.um.ltic.client.communication;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;

import uy.edu.um.ltic.commons.ConferenceRemoteMgt;
import uy.edu.um.ltic.commons.ConferenceVO;
import uy.edu.um.ltic.commons.EmployeeVO;
import uy.edu.um.ltic.commons.ExceptionComm;
import uy.edu.um.ltic.exception.DataBaseException;


public class ConferenceRemoteClientMgr implements ConferenceRemoteClientMgt{

	private static ConferenceRemoteClientMgr instance = null;

	private ConferenceRemoteClientMgr() throws RemoteException{
				
	}

	public static ConferenceRemoteClientMgr getInstance()  throws RemoteException{
		if (instance == null) {
			instance = new ConferenceRemoteClientMgr();
		}
		return instance;
	}
	
	
	@Override
	public void addConference(ConferenceVO oConfVO) throws ExceptionComm, DataBaseException {
		String sObjectService = "ConferenceMgt"; 
		Registry oRegitry;
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			ConferenceRemoteMgt oConferenceRemoteMgt = (ConferenceRemoteMgt) 
			oRegitry.lookup(sObjectService); 
			oConferenceRemoteMgt.addConference(oConfVO);;
			
		} catch (RemoteException | NotBoundException e) {
			throw new ExceptionComm(e);	
		} 
		
	}

	@Override
	public void deleteConference(ConferenceVO oConfVO) throws ExceptionComm, DataBaseException {
		String sObjectService = "ConferenceMgt"; 
		Registry oRegitry;
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			ConferenceRemoteMgt oConferenceRemoteMgt = (ConferenceRemoteMgt) 
			oRegitry.lookup(sObjectService); 
			oConferenceRemoteMgt.deleteConference(oConfVO);
			
		} catch (RemoteException | NotBoundException e) {
			throw new ExceptionComm(e);	
		} 
	}

	@Override
	public ArrayList<ConferenceVO> getConferencesImIn(EmployeeVO oEmployeeVO)
			throws ExceptionComm, DataBaseException {
		ArrayList<ConferenceVO> oListVO;
		String sObjectService = "ConferenceMgt"; 
		Registry oRegitry;
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			ConferenceRemoteMgt oConferenceRemoteMgt = (ConferenceRemoteMgt) 
			oRegitry.lookup(sObjectService); 
			oListVO = oConferenceRemoteMgt.getConferencesImIn(oEmployeeVO);
			
		} catch (RemoteException | NotBoundException e) {
			throw new ExceptionComm(e);	
		} 
		
		return oListVO;
	}

	@Override
	public ArrayList<ConferenceVO> getConferencesImTheOrganizer(
		EmployeeVO oEmployeeVO) throws ExceptionComm, DataBaseException {
		ArrayList<ConferenceVO> oListVO;
		String sObjectService = "ConferenceMgt"; 
		Registry oRegitry;
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			ConferenceRemoteMgt oConferenceRemoteMgt = (ConferenceRemoteMgt) 
			oRegitry.lookup(sObjectService); 
			oListVO = oConferenceRemoteMgt.getConferencesImTheOrganizer(oEmployeeVO);
			
		} catch (RemoteException | NotBoundException e) {
			throw new ExceptionComm(e);	
		} 
		
		return oListVO;
	}
	
	@Override
	public ArrayList<EmployeeVO> getAllParticipants(int idConference) throws ExceptionComm, DataBaseException{
		String sObjectService = "ConferenceMgt"; 
		Registry oRegitry;
		ArrayList<EmployeeVO> oListEmpl= new ArrayList<EmployeeVO>();
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			ConferenceRemoteMgt oConferenceRemoteMgt = (ConferenceRemoteMgt) 
			oRegitry.lookup(sObjectService); 
			oListEmpl = oConferenceRemoteMgt.getAllParticipants(idConference);
		} catch (RemoteException | NotBoundException e) {
			throw new ExceptionComm(e);	
		} 
		return oListEmpl;
	}

	@Override
	public String getConferenceName(int idConf) throws ExceptionComm, DataBaseException {
		String sObjectService = "ConferenceMgt"; 
		Registry oRegitry;
		String res = null;
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			ConferenceRemoteMgt oConferenceRemoteMgt = (ConferenceRemoteMgt) 
			oRegitry.lookup(sObjectService); 
			res = oConferenceRemoteMgt.getConferenceName(idConf);
		} catch (RemoteException | NotBoundException e) {
			throw new ExceptionComm(e);	
		} 
		return res;
	}


}
